package org.zzk.dao;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.Data;

@Data
public class ChessBoard {
    public static final int ROWS = 10;  // 棋盘行数（x轴：0-9）
    public static final int COLS = 9;   // 棋盘列数（y轴：0-8）
    private final Piece[][] board;      // board[x][y]

    public ChessBoard(Piece[][] board) {
        this.board = board;
    }

    public static ChessBoard initialBoard() {
        Piece[][] board = new Piece[ROWS][COLS];

        /* 红方（下方，x=0-4）*/
        // 第一行（x=0）：车马相士帅士相马车
        board[0][0] = new Piece(Piece.PieceType.CHARIOT, true);    // 车
        board[0][1] = new Piece(Piece.PieceType.HORSE, true);      // 马
        board[0][2] = new Piece(Piece.PieceType.ELEPHANT, true);   // 相
        board[0][3] = new Piece(Piece.PieceType.ADVISOR, true);    // 士
        board[0][4] = new Piece(Piece.PieceType.GENERAL, true);    // 帅
        board[0][5] = new Piece(Piece.PieceType.ADVISOR, true);    // 士
        board[0][6] = new Piece(Piece.PieceType.ELEPHANT, true);   // 相
        board[0][7] = new Piece(Piece.PieceType.HORSE, true);      // 马
        board[0][8] = new Piece(Piece.PieceType.CHARIOT, true);    // 车

        // 炮（x=2）
        board[2][1] = new Piece(Piece.PieceType.CANNON, true);     // 炮
        board[2][7] = new Piece(Piece.PieceType.CANNON, true);     // 炮

        // 兵（x=3）
        for (int y = 0; y < COLS; y += 2) {
            board[3][y] = new Piece(Piece.PieceType.SOLDIER, true); // 兵
        }

        /* 黑方（上方，x=5-9）*/
        // 最后一行（x=9）：车马象士将士象马车
        board[9][0] = new Piece(Piece.PieceType.CHARIOT, false);   // 车
        board[9][1] = new Piece(Piece.PieceType.HORSE, false);     // 马
        board[9][2] = new Piece(Piece.PieceType.ELEPHANT, false);  // 象
        board[9][3] = new Piece(Piece.PieceType.ADVISOR, false);   // 士
        board[9][4] = new Piece(Piece.PieceType.GENERAL, false);   // 将
        board[9][5] = new Piece(Piece.PieceType.ADVISOR, false);   // 士
        board[9][6] = new Piece(Piece.PieceType.ELEPHANT, false);  // 象
        board[9][7] = new Piece(Piece.PieceType.HORSE, false);     // 马
        board[9][8] = new Piece(Piece.PieceType.CHARIOT, false);   // 车

        // 炮（x=7）
        board[7][1] = new Piece(Piece.PieceType.CANNON, false);    // 炮
        board[7][7] = new Piece(Piece.PieceType.CANNON, false);    // 炮

        // 卒（x=6）
        for (int y = 0; y < COLS; y += 2) {
            board[6][y] = new Piece(Piece.PieceType.SOLDIER, false); // 卒
        }

        return new ChessBoard(board);
    }

    public Piece movePiece(ChessMove move) {
        Piece piece = board[move.getFromX()][move.getFromY()];
        Piece captured = board[move.getToX()][move.getToY()];
        board[move.getToX()][move.getToY()] = piece;
        board[move.getFromX()][move.getFromY()] = null;
        return captured;
    }

    public JsonObject toJson() {
        JsonObject jsonBoard = new JsonObject();
        JsonArray rows = new JsonArray();

        for (int x = 0; x < ROWS; x++) {
            JsonArray cols = getJsonArray(x);
            rows.add(cols);
        }

        jsonBoard.add("board", rows);
        jsonBoard.addProperty("rows", ROWS);
        jsonBoard.addProperty("cols", COLS);

        return jsonBoard;
    }

    private JsonArray getJsonArray(int x) {
        JsonArray cols = new JsonArray();
        for (int y = 0; y < COLS; y++) {
            JsonObject pieceJson = new JsonObject();
            Piece piece = board[x][y];

            if (piece != null) {
                pieceJson.addProperty("type", piece.getType().name());
                pieceJson.addProperty("chineseName", piece.getChineseName());
                pieceJson.addProperty("isRed", piece.isRed());
                // 可添加更多棋子属性
            } else {
                pieceJson.addProperty("empty", true);
            }
            cols.add(pieceJson);
        }
        return cols;
    }
}
